<?php
namespace Home\Controller;
use Think\Controller;

Class TongjiController extends CommonController{

    public function _initialize() {
        parent::_initialize();
        $this->dbname = CONTROLLER_NAME;
    }

   public function index(){
        $this->display();
   }

   public function type3(){
        $returnBack = $this->returnBack();
        $val = $this->details($returnBack['unReturnBack']);
        $zong = $returnBack['result'] + $returnBack['unReturnBack']['yanqi'];
        $data = array($zong,$returnBack['result'],$val['include'],$val['noInclude'],$val['yq']);
        $legend = array('总数','回款','有记录','未记录','逾期');
        $this->yqfenxi('type','放款统计',2,$data,$legend);
   }

   //获取有记录  没有记录  逾期   type3使用
   public function details($returnBack){
      $exist = $this->condition($returnBack['noOperate']['include']);
      $existYQ = count($exist['0-3']) + count($exist['3-7']) + count($exist['7-10']) + count($exist['10-30']) + count($exist['>30']);
      $notexist = $this->condition($returnBack['noOperate']['noInclude']);
      $notexistYQ = count($notexist['0-3']) + count($notexist['3-7']) + count($notexist['7-10']) + count($notexist['10-30']) + count($notexist['>30']);
      $arr['include'] = count($exist['>0']);
      $arr['noInclude'] = count($notexist['>0']);
      $arr['yq'] = $existYQ + $notexistYQ;
      return $arr;
   }

   public function type2(){
      $val = $this->includeYQ();
      $data = array(count($val['0-3']),count($val['3-7']),count($val['7-10']),count($val['10-30']),count($val['>30']));
      $legend = array('0-3','3-7','7-10','10-30','>30');
      $this->yqfenxi('type','逾期订单',2,$data,$legend);
   }

    public function type(){
      $val = $this->includeYQ();
      $data = array(count($val['0-3']),count($val['3-7']),count($val['7-10']),count($val['10-30']),count($val['>30']));
      $legend = array('0-3','3-7','7-10','10-30','>30');
      $this->yqfenxi('type','逾期订单',1,$data,$legend);
   }
  //统计到当前时间收回款的单子总数
   public function returnBack(){
    $arr['result'] = M('cash_order')->where("status = 5")->count();
    $arr['unReturnBack'] = $this->unReturnBack();
   // $arr['fkPass'] = $this->fkPass();
    return $arr;
   }

   //统计到当前时间未回款的订单  包括续期
   public function unReturnBack(){
    $result = M('cash_order')->where('status = 4')->select();
    $noOperate = $this->noOperate($result);
    $arr['yanqi'] = count($result);
    $arr['noOperate'] = $noOperate;
    return $arr;
   }

  //查询出没有任何交易的订单
   public function noOperate($val){
    foreach($val as $key=>$v){
      $result = M('cash_bf_dk')->where("order_id = {$v['order_id']} AND status = 'S'")->select();
      if($result != NULL){
        $arr['include'][] = $v;
      }else{
        //从未产生过交易记录的订单
        $arr['noInclude'][] = $v;
      }
    }
    //var_dump(count($arr['include']));exit;
        return $arr;
   }


   //从延期状态中删选出有记录的订单和没有记录的订单   再分别删选出逾期的天数
   public function includeYQ(){
    $result = $this->unReturnBack();
    //有记录
    $arr['yes'] = $this->condition($result['noOperate']['include']);

    //没有记录
    $arr['no'] = $this->condition($result['noOperate']['noInclude']);
    $arr_merge = array_merge_recursive($arr['yes'],$arr['no']);
    return $arr_merge;
   }

   public function condition($val){
    foreach($val as $key=>$v){
      if(overdue($v['expire_time'])>=0){
        $arr['>0'][] = $v;
      }elseif(overdue($v['expire_time'])<0 && overdue($v['expire_time'])>=-3){
        $arr['0-3'][] = $v;
      }elseif(overdue($v['expire_time'])<-3 && overdue($v['expire_time'])>=-7){
        $arr['3-7'][] = $v;
      }elseif(overdue($v['expire_time'])<-7 && overdue($v['expire_time'])>=-10){
        $arr['7-10'][] = $v;
      }elseif(overdue($v['expire_time'])<-10 && overdue($v['expire_time'])>=-30){
        $arr['10-30'][] = $v;
      }else{
        $arr['>30'][] = $v;
      }
    }

    return $arr;
   }

   //查询所有放款成功的订单
   public function fkPass(){

    $fk = M('cash_order')->join("cash_bf_dk ON cash_order.order_id = cash_bf_dk.order_id")->where("cash_order.status = 4 AND cash_bf_dk.trans_type = 3 AND cash_bf_dk.status = 'S'")->select();
    $result = $this->fkYan($fk); 
    return $result;
   }

   //从未还还款的订单中查询订单的延期情况
   public function fkYan($fk){
        for($i=0;$i<count($fk);$i++){
          $day = (($fk[$i]['k_time'] - $fk[$i]['loan_time']) / 86400);
          if($day > $fk[$i]['days']){
            $where['order_id'] = ['EQ',$fk[$i]['order_id']];
            $where['k_time'] = ['LT',$fk[$i]['k_time']];
            $where['status'] = ['eq','S'];
            $result = M('cash_bf_dk')->where($where)->order('k_time desc')->find();
            if($result['k_time'] > 0){
              //有续期
              $time = (($fk[$i]['k_time'] - $result['k_time']) / 86400);
              $delay = ceil($time - $result['over_day']);
            }else{
              //没有续期记录
              $time = (($fk[$i]['k_time'] - $fk[$i]['loan_time']) / 86400);
              $delay = ceil($time - $fk[$i]['days']);
            }
          }else{
              $delay = ceil($day - $fk[$i]['days']);
          }
          if($delay <= 0){
            $arr['<0'][] = $fk[$i];
          }elseif($delay>0 && $delay<=3){
            $arr['0-3'][] = $fk[$i];
          }elseif($delay>3 && $delay<=7){
            $arr['3-7'][] = $fk[$i];
          }elseif($delay>7 && $delay<=10){
            $arr['7-10'][] = $fk[$i];
          }elseif($delay>10 && $delay<=30){
            $arr['10-30'][] = $fk[$i];
          }else{
            $arr['>30'][] = $fk[$i];
          }
      }
       $array['<0'] = count($arr['<0']);
       $array['0-3'] = count($arr['0-3']);
       $array['3-7'] = count($arr['3-7']);
       $array['7-10'] = count($arr['7-10']);
       $array['10-30'] = count($arr['10-30']);
       $array['>30'] = count($arr['>30']);
       return $array;
   }


   public function yqfenxi($fd,$ft,$type,$data,$legend) {
        import("Org.Util.Chart");
        $chart = new \Chart;
        $title = $ft; 
       // $zong = $returnBack['result'] + $returnBack['unReturnBack']['yanqi'];
        //$data = array($zong,$returnBack['result'],$returnBack['unReturnBack']['yanqi'],count($returnBack['unReturnBack']['noOperate']['include']),count($returnBack['unReturnBack']['noOperate']['noInclude'])); 
        $size = 140; 
        $width = 550; 
        $height = 300; 
        
        ob_end_clean();
         if ($type == 1) {
            $chart->create3dpie($title,$data,$size,$height,$width,$legend);
         }
         if ($type == 2) {
            $chart->createcolumnar($title,$data,$size,$height,$width,$legend);
         }
         if ($type == 3) {
            $chart->createmonthline($title,$data,$size,$height,$width,$legend);
         }
         if ($type == 4) {
            $chart->createring($title,$data,$size,$height,$width,$legend);
         }
         if ($type == 5) {
            $chart->createhorizoncolumnar($title,$subtitle,$data,$size,$height,$width,$legend);
         }
   
    }

}